﻿using Ninject;
using Ninject.Modules;
using NServiceKit.Redis;
using System.Data.Entity;

namespace HobSoft.SoccerBet2014.Server
{
    using Domain.ClientService;
    using Domain.Common.Site;
    using Domain.Common.SiteContext;
    using Domain.DataService;
    using Domain.IRepositories;
    using Domain.Models.DbContext;
    using dm = Domain;
    using dmc = Domain.Common;
    using impl = Domain.Implementation;

    public class RepositoryIocModule : NinjectModule
    {
        public static string[] redisServer = new string[] { "localhost" };
        public static RedisClientManagerConfig RedisConfig = new RedisClientManagerConfig()
        {
            MaxReadPoolSize = 20,
            MaxWritePoolSize = 20,
            AutoStart = true
        };

        public override void Load()
        {
            #region repository context
            Bind<PooledRedisClientManager>()
                .ToSelf()
                .InSingletonScope()
                .WithConstructorArgument("readWriteHosts", redisServer)
                .WithConstructorArgument("readOnlyHosts", redisServer)
                .WithConstructorArgument("config", RedisConfig);

            Database.SetInitializer<BetDbContext>(null);
            Database.SetInitializer<BFDbContext>(null);
            Bind<DbContext>()
                .To<BFDbContext>()
                .WhenInjectedInto(typeof(impl.SqlRepository.GameMappingRepository)
                                            , typeof(impl.SqlRepository.BFEventRepository)
                                            , typeof(impl.SqlRepository.BFMarketRepository)
                                            , typeof(impl.SqlRepository.BFRunnerRepository));
            Bind<DbContext>()
                .To<BetDbContext>()
                .WhenInjectedInto(typeof(impl.SqlRepository.BookmakerRepository)
                                            , typeof(impl.SqlRepository.GameRepository)
                                            , typeof(impl.SqlRepository.MarketRepository)
                                            , typeof(impl.SqlRepository.OddsRepository));
            #endregion

            #region repository
            Bind<ICookiesManager>()
                .To<impl.RedisRepository.CookiesManager>()
                .InSingletonScope();
            Bind<ISiteContextRepository>()
                .To<impl.RedisRepository.SiteContextRepository>()
                .InSingletonScope();

            Bind<IBFEventRepository>()
                .To<impl.RedisRepository.BFEventRepository>()
                .WhenMemberHas<impl.Service.CacheAttribute>()
                .InSingletonScope();
            Bind<IBFMarketRepository>()
                .To<impl.RedisRepository.BFMarketRepository>()
                .WhenMemberHas<impl.Service.CacheAttribute>()
                .InSingletonScope();
            Bind<IBookmakerRepository>()
                .To<impl.RedisRepository.BookmakerRepository>()
                .WhenMemberHas<impl.Service.CacheAttribute>()
                .InSingletonScope();
            Bind<IGameMappingRepository>()
                .To<impl.RedisRepository.GameMappingRepository>()
                .WhenMemberHas<impl.Service.CacheAttribute>()
                .InSingletonScope();
            Bind<IGameRepository>()
                .To<impl.RedisRepository.GameRepository>()
                .WhenMemberHas<impl.Service.CacheAttribute>()
                .InSingletonScope();
            Bind<IMarketRepository>()
                .To<impl.RedisRepository.MarketRepository>()
                .WhenMemberHas<impl.Service.CacheAttribute>()
                .InSingletonScope();
             Bind<IOddsRepository>()
                .To<impl.RedisRepository.OddsRepository>()
                .WhenMemberHas<impl.Service.CacheAttribute>()
                .InSingletonScope();

            Bind<IBookmakerRepository>()
                .To<impl.RedisRepository.BookmakerRepository>()
                .WhenInjectedInto(typeof(IBetGameDataService), typeof(IBetCustomDataService), typeof(IBfCustomDataService), typeof(ISystemService))
                .InSingletonScope();
            Bind<IBFEventRepository>()
                .To<impl.RedisRepository.BFEventRepository>()
                .WhenInjectedInto(typeof(IBetCustomDataService), typeof(IBfCustomDataService))
                .InSingletonScope();
            Bind<IBFMarketRepository>()
                .To<impl.RedisRepository.BFMarketRepository>()
                .WhenInjectedInto(typeof(IBetCustomDataService), typeof(IBfCustomDataService))
                .InSingletonScope();
            Bind<IGameMappingRepository>()
                .To<impl.RedisRepository.GameMappingRepository>()
                .WhenInjectedInto(typeof(IBetGameDataService), typeof(IBetCustomDataService), typeof(IBfCustomDataService))
                .InSingletonScope();
            Bind<IGameRepository>()
                .To<impl.RedisRepository.GameRepository>()
                .WhenInjectedInto(typeof(IBetGameDataService), typeof(IBetCustomDataService), typeof(IBetSiteManagerService))
                .InSingletonScope();
            Bind<IMarketRepository>()
                .To<impl.RedisRepository.MarketRepository>()
                .WhenInjectedInto(typeof(IBetGameDataService), typeof(IBetCustomDataService), typeof(IBetSiteManagerService))
                .InSingletonScope();
            Bind<IOddsRepository>()
                .To<impl.RedisRepository.OddsRepository>()
                .WhenInjectedInto(typeof(IBetGameDataService), typeof(IBetCustomDataService), typeof(IBetSiteManagerService))
                .InSingletonScope();
            Bind<IBFEventRepository>()
                .To<impl.RedisRepository.BFEventRepository>()
                .WhenInjectedInto(typeof(IBetGameDataService), typeof(IBetCustomDataService), typeof(IBetSiteManagerService))
                .InSingletonScope();

            Bind<IBookmakerRepository>()
                .To<impl.SqlRepository.BookmakerRepository>()
                .InSingletonScope();

            Bind<IGameMappingRepository>()
                .To<impl.SqlRepository.GameMappingRepository>()
                .WhenMemberHas<impl.Service.DbAttribute>()
                .InSingletonScope();
            Bind<IBFEventRepository>()
                .To<impl.SqlRepository.BFEventRepository>()
                .WhenMemberHas<impl.Service.DbAttribute>()
                .InSingletonScope();
            Bind<IBFMarketRepository>()
                .To<impl.SqlRepository.BFMarketRepository>()
                .WhenMemberHas<impl.Service.DbAttribute>()
                .InSingletonScope();
            Bind<IGameRepository>()
                .To<impl.SqlRepository.GameRepository>()
                .WhenMemberHas<impl.Service.DbAttribute>()
                .InSingletonScope();
            Bind<IMarketRepository>()
                .To<impl.SqlRepository.MarketRepository>()
                .WhenMemberHas<impl.Service.DbAttribute>()
                .InSingletonScope();
             Bind<IOddsRepository>()
                .To<impl.SqlRepository.OddsRepository>()
                .WhenMemberHas<impl.Service.DbAttribute>()
                .InSingletonScope();

            //Bind<IBFEventRepository>()
            //    .To<impl.SqlRepository.BFEventRepository>()
            //    .WhenInjectedInto(typeof(IBfEventService))
            //    .InSingletonScope();
           //     .WithConstructorArgument("dbContext", ctx => ctx.Kernel.Get<DbContext>());
            #endregion

            //service
            #region service
            Bind<IBetGameDataService>()
                .To<dmc.Service.BetGameDataService>()
                .InSingletonScope();

            Bind<IBetSettingService>()
                .To<impl.Service.BetSettingService>()
                .InSingletonScope();

            Bind<impl.Service.PersistentModelsService>()
                .ToSelf()
                .InSingletonScope();

            Bind<ISystemService>()
                .To<Wcf.SystemService>();
            Bind<IBetCustomDataService>()
                .To<Wcf.BetCustomDataService>();
            Bind<IBfCustomDataService>()
                .To<Wcf.BfCustomDataService>();
            #endregion

            #region site service
            Bind<IbcSessionManager>()
                .ToSelf();
            Bind<SboSessionManager>()
                .ToSelf();
            Bind<HgSessionManager>()
                .ToSelf();


            Bind<dm.Schedule.ITimer>()
                .To<dmc.Schedule.ServiceTimer>();

            Bind<dm.Factory.IBetSiteManagerFactory>()
                .To<dmc.Factory.SiteManagerFactory>()
                .InSingletonScope();
            Bind<dm.Factory.IScheduleTimerFactory>()
                .To<dmc.Factory.ScheduleTimerFactory>()
                .InSingletonScope();

            ContextManager.Repository = Kernel.Get<ISiteContextRepository>();
            #endregion

        }
    }
}
